package com.ll.domain.ranking.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ll.app.api.req.PageRankingReqDTO;
import com.ll.base.dto.PageResultDTO;
import com.ll.base.util.ListUtil;
import com.ll.dal.mapper.RankingMapper;
import com.ll.dal.po.RankingPO;
import com.ll.domain.ranking.base.RankingBaseService;
import com.ll.domain.ranking.convert.RankingConvert;
import com.ll.domain.ranking.dto.RankingDTO;
import com.ll.domain.ranking.enums.RankingStatusEnum;
import com.ll.domain.ranking.enums.RankingTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RankingBaseServiceImpl extends ServiceImpl<RankingMapper, RankingPO> implements RankingBaseService {

    private final RankingMapper rankingMapper;

    @Override
    public Long getRanking(Long tenantId, Long uid, RankingTypeEnum type){
       return rankingMapper.getRanking(tenantId, uid, type.name());
    }

    @Override
    public RankingPO getRankingOne(Long tenantId, Long uid, RankingTypeEnum type, RankingStatusEnum status) {
        return rankingMapper.selectOne(new LambdaQueryWrapper<RankingPO>()
        .eq(RankingPO::getTenantId, tenantId)
                .eq(RankingPO::getStudentUid, uid)
                .eq(RankingPO::getType, type)
                .eq(RankingPO::getStatus, status));
    }

    @Override
    public void authRanking(Long id,Long teacherId,Long sort ) {
        RankingPO po1 = new RankingPO();
        po1.setId(id);
        po1.setStatus(RankingStatusEnum.已认证);
        po1.setTeacherUid(teacherId);
        po1.setSort(sort);
        po1.setUtime(new Date());
        rankingMapper.updateById(po1);
    }

    @Override
    public  PageResultDTO<RankingDTO>  page(PageRankingReqDTO pageReq){
        pageReq.initPage();
        if(pageReq.getStatus() == null){
            pageReq.setStatus(RankingStatusEnum.已认证);
        }

        IPage<RankingPO> iPage = new Page<RankingPO>(pageReq.getPageNo(), pageReq.getPageSize());
        iPage = rankingMapper.selectPage(iPage, new LambdaQueryWrapper<RankingPO>()
                .eq(RankingPO::getType, pageReq.getType())
                .eq(RankingPO::getStatus, pageReq.getStatus())
                .orderByDesc(RankingPO::getSort)
        );
        List<RankingDTO> list = ListUtil.toList(iPage.getRecords(), RankingConvert::PO2DTO);
       return new PageResultDTO<RankingDTO>(list, iPage.getTotal(), pageReq);
    }

    @Override
    public void cleanRanking() {
        RankingPO po = new RankingPO();
        po.setStatus(RankingStatusEnum.赛季结束);
        rankingMapper.update(po,new LambdaQueryWrapper<RankingPO>().eq(RankingPO::getStatus, RankingStatusEnum.已认证));
    }

}
